﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GoodMan.Core.HitMonitor;
using GoodMan.Infrastructure.Entities;
using GoodMan.Infrastructure.Entities.Repository;
using GoodMan.Tool.Framework;
using System.Web;
using System.Text.RegularExpressions;

namespace GoodMan.Core.HitMonitor
{
    public class InfoHitMonitor :BaseMonitor
    {
        public InfoHitMonitor(object key) : base(key) { }

        internal static string GetMemoryKey(object key)
        { 
            return string.Format("HitMonitor_of_Info_id:{0}", key);
        }

        protected override void LoadAction()
        {
            if (key != null)
            {
                using (Context ctx = new Context())
                { 
                    int id = Int32.Parse(key.ToString());
                    var time = (from c in ctx.Information
                                where c.Id == id
                                select new { c.CTR }).FirstOrDefault();
                    if (time != null)
                    {
                        string host = HttpContext.Current.Request.Url.Host;
                        string contextName = "name=Context";
                        if (new Regex("([A-Za-z0-9\\-]+\\.){2}com").IsMatch(host))
                        {
                            contextName = "name=" + host.Substring(0, host.IndexOf('.'));
                        }
                        this.OriginalValue = new InfoHitPack()
                        {
                            CTR = time.CTR,
                            DBName = contextName
                        };
                        this.CurrentValue = new InfoHitPack()
                        {
                            CTR = time.CTR,
                            DBName = contextName
                        };
                    }
                }
            }
        }

        internal override void Flush()
        {
            if (OriginalValue != null && CurrentValue != null)
            {
                var currentObject = CurrentValue as InfoHitPack;
                var originalObject = OriginalValue as InfoHitPack;
                if (currentObject.CTR != originalObject.CTR)
                {
                    using (Context ctx = new Context(currentObject.DBName))
                    {
                        try
                        {
                            ctx.Update<T_Information>()
                                .Set(c => c.CTR, currentObject.CTR)
                                .WhereSet(c => c.Id, WhereOperator.Equal, key)
                                .End()
                                .Excute();
                            originalObject.CTR = currentObject.CTR;
                        }
                        catch (Exception e)
                        {
                            LogHelper.Write(CommonLogger.ExServer, LogLevel.Error, e);
                        }
                    }
                }
            }
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="action">1:visit;</param>
        public override void Trigger(int action,int up=1)
        {
            if (CurrentValue == null)
            {
                LoadProperties();
            }
            if (this.CurrentValue != null)
            {
                var currentObject = CurrentValue as InfoHitPack;
                var actionName = (InfoGetAction)action;
                if (actionName == InfoGetAction.visit)
                {
                    currentObject.CTR += up;
                }
            }
        }

        public override void Dispose()
        {
            Flush();
        }
    }

    public class InfoHitPack
    {
        public int CTR;
        public string DBName;
    }
    public enum InfoGetAction
    { 
        visit = 1
    }
}
